package com.tedu.pj.sys.service.impl;

import com.tedu.pj.common.exception.ServiceException;
import com.tedu.pj.common.pojo.CheckBox;
import com.tedu.pj.common.pojo.PageObject;
import com.tedu.pj.sys.dao.SysRoleDao;
import com.tedu.pj.sys.dao.SysRoleMenuDao;
import com.tedu.pj.sys.dao.SysUserRoleDao;
import com.tedu.pj.sys.pojo.SysRole;
import com.tedu.pj.sys.pojo.SysRoleMenuVo;
import com.tedu.pj.sys.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Song
 * @since 2020/10/19
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleDao sysRoleDao;

    @Autowired
    private SysRoleMenuDao sysRoleMenuDao;
    @Autowired
    private SysUserRoleDao sysUserRoleDao;

    @Override
    public int updateObject(SysRole entity, Integer[] menuIds) {
        if (entity == null) {
            throw new IllegalArgumentException("更新的对象不能为空");
        }
        if (entity.getId() == null) {
            throw new IllegalArgumentException("id的值不能为空");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new IllegalArgumentException("角色名不能为空");
        }
        if (menuIds == null || menuIds.length == 0) {
            throw new IllegalArgumentException("必须为角色指定一个权限");
        }
        int rows = sysRoleDao.updateObject(entity);
        if (rows == 0) {
            throw new ServiceException("对象可能已经不存在");
        }
        sysRoleMenuDao.deleteObjectsByRoleId(entity.getId());
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

    @Override
    public SysRoleMenuVo findObjectById(Integer id) {
        //1.合法性验证
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("id的值不合法");
        }
        SysRoleMenuVo result = sysRoleDao.findObjectById(id);
        if (result == null) {
            throw new ServiceException("此记录已经不存在");
        }
        return result;
    }

    @Override
    public List<CheckBox> findObjects() {
        return sysRoleDao.findObjects();
    }

    @Override
    public int saveObject(SysRole entity, Integer[] menuIds) {
        //1.参数有效性校验
        if (entity == null) {
            throw new IllegalArgumentException("保存对象不能为空");
        }
        if (StringUtils.isEmpty(entity.getName())) {
            throw new IllegalArgumentException("角色名不允许为空");
        }
        if (menuIds == null || menuIds.length == 0) {
            throw new ServiceException("必须为角色分配权限");
        }
        int rows = sysRoleDao.insertObject(entity);
        sysRoleMenuDao.insertObjects(entity.getId(), menuIds);
        return rows;
    }

    @Override
    public int deleteObject(Integer id) {
        //1.验证数据的合法性
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("请先选择");
        }
        sysRoleMenuDao.deleteObjectsByRoleId(id);
        int rows = sysRoleDao.deleteObject(id);
        if (rows == 0) {
            throw new ServiceException("此记录可能已经不存在");
        }
        return rows;
    }

    @Override
    public PageObject<SysRole> findPageObjects(String name, Integer pageCurrent) {
        if (pageCurrent == null || pageCurrent < 1) {
            throw new IllegalArgumentException("当前页码值无效");
        }
        int rowCount = sysRoleDao.getRowCount(name);
        if (rowCount == 0) {
            throw new ServiceException("没有找到对应记录");
        }
        int pageSize = 10;
        int startIndex = (pageCurrent - 1) * pageSize;
        List<SysRole> records = sysRoleDao.findPageObjects(name, startIndex, pageSize);
        return new PageObject<SysRole>(pageCurrent, rowCount, pageSize, records);
    }


}
